//
//  WKWebView+LF.m
//  AntFarms
//
//  Created by LF on 2017/12/22.
//  Copyright © 2017年 MuXi. All rights reserved.
//

#import "WKWebView+LF.h"
#import "ZLPhotoPickerBrowserViewController+LF.h"

@implementation WKWebView (LF)
- (void)insertJSCode {
    
    //js方法遍历图片添加点击事件 返回图片个数
    static  NSString * const jsGetImages =
    @"function getImages(){\
    var objs = document.getElementsByTagName(\"img\");\
    for(var i=0;i<objs.length;i++){\
    objs[i].onclick=function(){\
    document.location.href=\"jscallbackoc://saveImage_*\"+this.src;\
    };\
    };\
    return objs.length;\
    };";
    
    [self evaluateJavaScript:jsGetImages completionHandler:^(id _Nullable data, NSError * _Nullable error) {
        
    }];
    
//    [self stringByEvaluatingJavaScriptFromString:jsGetImages];//注入js方法
    
    //注入自定义的js方法后别忘了调用 否则不会生效
    [self evaluateJavaScript:@"getImages()" completionHandler:^(id _Nullable data, NSError * _Nullable error) {
        
    }];//调用js方法
    
//    [self stringByEvaluatingJavaScriptFromString:@"getImages()"];//调用js方法
    
    
    //这里是js，主要目的实现对url的获取
    static  NSString * const jsGetImgs =
    @"function getImgs(){\
    var objs = document.getElementsByTagName(\"img\");\
    var imgScr = '';\
    for(var i=0;i<objs.length;i++){\
    imgScr = imgScr + objs[i].src; \
    if (i < objs.length - 1) { \
    imgScr = imgScr + '+'; \
    } \
    };\
    return imgScr;\
    };";
    
    
    [self evaluateJavaScript:jsGetImgs completionHandler:^(id _Nullable data, NSError * _Nullable error) {
        
    }];
//    [self stringByEvaluatingJavaScriptFromString:jsGetImgs];//注入js方法
    
    
    //    // 禁用用户选择
    //    [self stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    
    // 禁用长按弹出框
    //    [self stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
    
}



- (NSArray *)performJSMethodWithURL:(NSString *)url protocolName:(NSString *)name {
    // 获得协议后面的路径为： sendMessage_number2_*200$300
    NSString *path = [url substringFromIndex:name.length];
    // 利用“*”切割路径，“*”前面是方法名，后面是参数
    NSArray *subpaths = [path componentsSeparatedByString:@"*"];
    
    
    // 参数  200$300，每个参数之间使用符号$链接（避免和url里面的其他字符混淆，因为一般url里面不会出现这个字符）
    NSArray *params = nil;
    if (subpaths.count == 2) {
        NSString *url = [subpaths lastObject];
        NSMutableString *imageUrl = [[NSMutableString alloc] initWithString:url];
        if (![url containsString:@":"]) {
            if ([url hasPrefix:@"https"]) {
                [imageUrl insertString:@":" atIndex:5];
            }else if ([url hasPrefix:@"http"]){
                [imageUrl insertString:@":" atIndex:4];
            }
        }
        
        params = [imageUrl componentsSeparatedByString:@"$"];
    }
    
//    NSString *urlResurlt = [self stringByEvaluatingJavaScriptFromString:@"getImgs()"];
    [self evaluateJavaScript:@"getImgs()" completionHandler:^(id _Nullable data, NSError * _Nullable error) {
        NSMutableArray *arr = [NSMutableArray arrayWithArray:[data componentsSeparatedByString:@"+"]];
        
        if (params.count > 0) {
            NSString *currentImg = params[0];
            if ([arr containsObject:currentImg]) {
                [ZLPhotoPickerBrowserViewController showPhotoBrowserInViewController:nil withImages:arr atIndex:[arr indexOfObject:currentImg]];
            } else {
                [ZLPhotoPickerBrowserViewController showPhotoBrowserInViewController:nil withImages:params atIndex:0];
            }
        }
    }];
    
//    NSMutableArray *arr = [NSMutableArray arrayWithArray:[urlResurlt componentsSeparatedByString:@"+"]];
//
//    if (params.count > 0) {
//        NSString *currentImg = params[0];
//        if ([arr containsObject:currentImg]) {
//            [ZLPhotoPickerBrowserViewController showPhotoBrowserInViewController:nil withImages:arr atIndex:[arr indexOfObject:currentImg]];
//        } else {
//            [ZLPhotoPickerBrowserViewController showPhotoBrowserInViewController:nil withImages:params atIndex:0];
//        }
//    }
    
    
    return params;
}

@end
